﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SolutionModel;
using System.Data.SqlClient;
using System.Data;
using SFast.DB;
using SFast;
using SolutionDAL;
namespace SolutionBLL
{
    public class T_MenuBLL
    {
        #region 获取分类树

        /// <summary>
        /// 获取分类树
        /// </summary>
        /// <returns></returns>
        public List<TreeGridModel> GetTree()
        {
            return GetTree(0, 0);
        }
        #endregion

        #region 获取分类树 可以控制显示最大级别
        /// <summary>
        /// 获取分类树 可以控制显示最大级别
        /// </summary>
        /// <param name="MaxDepth">最大深度（不可高于，可等于此级别的分类）</param>
        /// <returns></returns>
        public List<TreeGridModel> GetTree(int MaxDepth)
        {
            return GetTree(0, MaxDepth);
        }
        #endregion

        #region 获取某段级别范围的分类树 可以控制显示最小和最大级别
        /// <summary>
        /// 获取分类树 可以控制显示最大级别
        /// </summary>
        /// <param name="MinDepth">最小深度（不可低于，可等于此级别的分类）</param>
        /// <param name="MaxDepth">最大深度（不可高于，可等于此级别的分类）</param>
        /// <returns></returns>
        public List<TreeGridModel> GetTree(int MinDepth, int MaxDepth)
        {
            T_MenuDAL dal = new T_MenuDAL();
            List<T_Menu> menuList = dal.GetTree(MinDepth, MaxDepth);
            List<TreeGridModel> listModel = new List<TreeGridModel>();
            menuList.ForEach(o =>
            {
                listModel.Add(new TreeGridModel()
                    {
                        ID = o.C_ID,
                        Name = o.C_Name,
                        EnName = o.C_EnName,
                        Url = o.C_Url,
                        PID = o.C_Parent_ID,
                        Path = o.C_Path,
                        Depth = o.C_Depth,
                        Order = o.C_Order,
                        Tools = String.Format(@"<span style='cursor: pointer; margin: 0px 30px 0px 10px' onclick='EditAlter({1})'>编辑</span>
                                                <span style='cursor: pointer;' onclick='deleteAlter({1})'>删除</span>", o.C_Parent_ID, o.C_ID)
                    });
            });
            listModel.ForEach(o =>
            {
                o.children = listModel.Where(p => p.PID == o.ID).ToList<TreeGridModel>();
            });
            listModel = listModel.Where(o => o.Depth == 1).ToList();
            return listModel;
        }
        #endregion

        #region 获取所有菜单
        /// <summary>
        /// 获取所有菜单
        /// </summary>
        /// <returns></returns>
        public List<T_Menu> getAllTree()
        {
            T_MenuDAL dal = new T_MenuDAL();
            List<T_Menu> menuList = dal.GetTree(0, 0);
            return menuList;
        }
        #endregion

        #region 获取不包含自己本身以及子类的分类树，用于分类编辑时
        /// <summary>
        /// 获取不包含自己本身以及子类的分类树，用于分类编辑时
        /// </summary>
        /// <param name="CurrentID"></param>
        /// <returns></returns>
        public List<TreeGridModel> GetTreeNotInCurrent(String CurrentID)
        {
            T_MenuDAL dal = new T_MenuDAL();
            List<T_Menu> menuList = dal.GetTree();
            List<TreeGridModel> listModel = new List<TreeGridModel>();
            menuList.ForEach(o =>
            {
                listModel.Add(new TreeGridModel()
                {
                    ID = o.C_ID,
                    Name = o.C_Name,
                    EnName = o.C_EnName,
                    Url = o.C_Url,
                    PID = o.C_Parent_ID,
                    Path = o.C_Path,
                    Depth = o.C_Depth,
                    Order = o.C_Order
                });
            });
            IEnumerable<TreeGridModel> list = listModel.Where(o => !("|" + o.Path + "|").Contains("|" + CurrentID + "|"));
            list.ForEach(o =>
            {
                o.children = list.Where(p => p.PID == o.ID).ToList<TreeGridModel>();
            });
            list = list.Where(o => o.Depth == 1);
            return list.ToList();
        }
        #endregion

        #region  添加菜单
        /// <summary>
        /// 添加菜单
        /// 标记：需要修改
        /// </summary>
        /// <param name="obj"></param>
        public int InsertMenus(T_Menu obj)
        {
            T_MenuDAL dal = new T_MenuDAL();
            return dal.InsertMenus(obj);
        }

        #endregion

        #region  通过菜单ID获取菜单
        /// <summary>
        /// 通过菜单ID获取菜单
        /// </summary>
        /// <param name="mid">菜单ID</param>
        /// <returns></returns>
        public IQueryable<T_Menu> GetMenuByID(int mid)
        {
            T_MenuDAL dal = new T_MenuDAL();
            return dal.GetMenuByID(mid);
        }

        #endregion

        #region  通过ID获取菜单并转为JS，用于回填写到表单中
        /// <summary>
        /// 通过ID获取菜单并转为JS，用于回填写到表单中
        /// 标记：需要修改
        /// </summary>
        /// <param name="mid">菜单ID</param>
        /// <returns></returns>
        public void GetMenuByIDToJs(int mid)
        {
            T_MenuDAL dal = new T_MenuDAL();
            dal.GetMenuByIDToJs(mid);
        }

        #endregion

        #region 通过父类获取第一级子级别的分类树
        /// <summary>
        /// 通过父类获取第一级子级别的分类树
        /// </summary>
        /// <param name="pid">父ID</param>
        /// <returns></returns>
        public IQueryable<T_Menu> GetTreeByPid(int pid)
        {
            T_MenuDAL dal = new T_MenuDAL();
            return dal.GetTreeByPid(pid);
        }

        #endregion

        #region 通过ID更新分类数据
        /// <summary>
        /// 标记需要修改
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public int UpdateMenu(T_Menu obj)
        {
            T_MenuDAL dal = new T_MenuDAL();
            return dal.UpdateMenu(obj);
        }

        #endregion

        #region 同级排序

        /// <summary>
        /// 同级排序
        /// 标记，需要修改
        /// </summary>
        /// <param name="cid">当前需要变换位置的菜单ID</param>
        /// <param name="targetid">新位置的ID，也就是说，当前ID所要替换谁新位置的ID的位置</param>
        public void SortMenu(int cid, int targetid,int pid )
        {
            T_MenuDAL dal = new T_MenuDAL();
            dal.SortMenu(cid, targetid, pid);
        }

        #endregion

        #region 判断当前的菜单父ID是否为自己或自己的所有子菜单ID
        /// <summary>
        /// 编辑时，选择父类菜单时， 判断当前的选择项是否为自己或自己的所有子菜单ID
        /// 不可把“菜单本身”或“子菜单”当做本菜单的“父菜单”，请重新选择父菜单项！
        /// </summary>
        /// <param name="ParentID">父ID</param>
        /// <param name="MenuID">当前菜单ID</param>
        /// <returns></returns>
        public int VerifyParent(int ParentID, int MenuID)
        {
            T_MenuDAL dal = new T_MenuDAL();
            return dal.VerifyParent(ParentID, MenuID);
        }
        #endregion

        #region 删除菜单
        /// <summary>
        /// 删除菜单
        /// </summary>
        /// <param name="menuID"></param>
        public void delMenuID(int menuID)
        {
            T_MenuDAL dal = new T_MenuDAL();
            dal.delMenuID(menuID);
        }
        #endregion

        #region 通过用户ID获取该用户有权限访问的菜单列表
        /// <summary>
        /// 通过用户ID获取该用户有权限访问的菜单列表
        /// </summary>
        /// <param name="uid"></param>
        /// <returns></returns>
        public List<T_Menu> getMenuByUsesrID(String uid)
        {
            T_MenuDAL dal = new T_MenuDAL();
            return dal.getMenuByUsesrID(uid);
        }
        #endregion

    }
}

